package cn.trip.service.provider;


import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.trip.dao.provider.ProviderMapper;
import cn.trip.pojo.Trip_bill;
import cn.trip.pojo.Trip_product;
import cn.trip.pojo.Trip_provider;
import cn.trip.pojo.Trip_room;

@Service("providerService")
public class ProviderServiceImpl implements ProviderService {

	@Resource
	private ProviderMapper providerMapper;
	@Override
	public Trip_provider login(String proCode, String password) {
		// TODO Auto-generated method stub
		Trip_provider provider = providerMapper.login(proCode);
		if (provider != null) {
			if (!provider.getPassword().equals(password)) {
				provider = null;
			}
		}
		return provider;
	}
	@Override
	public List<Trip_product> productlist(String proCode,String queryName,int currentPageNo, int pageSize) {
		// TODO Auto-generated method stub
		
		currentPageNo = (currentPageNo-1)*pageSize;
		return providerMapper.products(proCode, queryName,currentPageNo, pageSize);
	}
	@Override
	public int proCount(String proCode, String queryName) {
		// TODO Auto-generated method stub
		return providerMapper.proCount(proCode, queryName);
	}
	@Override
	public Trip_product getProduct(String id) {
		// TODO Auto-generated method stub
		return providerMapper.getProduct(id);
	}
	@Override
	public List<Trip_bill> brands(String proCode, String queryName1, Integer currentPageNo1, Integer pageSize) {
		// TODO Auto-generated method stub
		currentPageNo1 = (currentPageNo1-1)*pageSize;
		return providerMapper.brands(proCode, queryName1, currentPageNo1, pageSize);
	}
	@Override
	public int brandCount(String proCode, String queryName1) {
		// TODO Auto-generated method stub
		return providerMapper.brandCount(proCode, queryName1);
	}
	@Override
	public Trip_bill getBrand(String id) {
		// TODO Auto-generated method stub
		return providerMapper.getBrand(id);
	}
	@Override
	public boolean getdel(String p_id) {
		// TODO Auto-generated method stub
		if (providerMapper.getdel(p_id)>0) {
			return true;
		}
		return false;
	}
	@Override
	public boolean addpro(Trip_product product) {
		// TODO Auto-generated method stub
		if (providerMapper.addpro(product)>0) {
			return true;
		}
		return false;
	}
	@Override
	public boolean getproductByName(String proCode, String productName, String address) {
		// TODO Auto-generated method stub
		List<Trip_product> products = providerMapper.getproductByName(proCode, productName);
		if (products.size()>0) {//查询有这个商品名字，就比较一下它的地址
			for (Trip_product trip_product : products) {
				if (trip_product.getAddress().equals(address)) {
					return true; //表示真的存在
				}else {
					return false;//表示不存在
							
				}
				
			}
		}
		return false;
	}
	@Override
	public boolean modifyproduct(Trip_product product) {
		// TODO Auto-generated method stub
		if (providerMapper.modifyproduct(product)>0) {
			return true;//修改成功
		}
		return false;
	}
	@Override
	public boolean addRoom(Trip_room room) {
		// TODO Auto-generated method stub
		if (providerMapper.addRoom(room)>0) {
			return true;
		}
		return false;
	}
	
	//获取酒店房型信息
	public List<Trip_room>  roomList(String hotelName){
		return providerMapper.roomList(hotelName);
	}
	@Override
	public int modifyroom(Trip_room room) {
		// TODO Auto-generated method stub
		return providerMapper.modifyroom(room);
	}
	@Override
	public Trip_room getRoomById(String room_id) {
		// TODO Auto-generated method stub
		return providerMapper.getRoomById(room_id);
	}
	@Override
	public int delroom(String room_id) {
		// TODO Auto-generated method stub
		return providerMapper.delroom(room_id);
	}
	@Override
	public int delroomList(String hotelName) {
		// TODO Auto-generated method stub
		return providerMapper.delroomList(hotelName);
	}
	
	//注册
		public int dosign(Trip_provider provider) {
			return providerMapper.setPro(provider);
		}
		
		// 注册判断商家编号
		public String proCode() {
			// TODO Auto-generated method stub
			// 生成编号
			String range1 = "abcdefghijklmnopqrstuvwxyz";
			String range2 = "0123456789";
			Trip_provider provider = null;
			String proCode = null;
			while (true) {
				Random random = new Random();
				StringBuffer result = new StringBuffer();
				// 要生成几位，就把这里的数字改成几
				for (int i = 0; i < 2; i++) {
					result.append(range1.charAt(random.nextInt(range1.length())));
				}
				for (int i = 0; i < 4; i++) {
					result.append(range2.charAt(random.nextInt(range2.length())));
				}
				proCode = result.toString();
				provider = providerMapper.login(proCode);
				if (null == provider) {
					return proCode;
				}
			}
		}
		
		//ajax 商家名判断
		public boolean proName(String proName,String address){
			Trip_provider provider = providerMapper.getProName(proName);
			if (provider != null) {
				if (provider.getProAddress().equals(address)) {
					return false;//表示商家已经存在
				}else {
					return true;
				}
			}else {
				return true;//表示商家不存在
			}
			
			
			
		}
		
		// 根据编号查找
		public Trip_provider proCode(String proCode) {
			return providerMapper.login(proCode);
		}
		
		// 修改信息
			public int modify(Trip_provider provider) {
				return providerMapper.modify(provider);
			}
			@Override
			public List<Trip_bill> bills(String proCode, Integer isPay, Integer currentPageNo, Integer pageSize) {
				// TODO Auto-generated method stub
				currentPageNo = (currentPageNo-1)*pageSize;
				return providerMapper.bills(proCode, isPay, currentPageNo, pageSize);
			}
			@Override
			public int billCount(String proCode, Integer isPay) {
				// TODO Auto-generated method stub
				return providerMapper.billCount(proCode, isPay);
			}
			@Override
			public Trip_bill getBillById(String id) {
				// TODO Auto-generated method stub
				return providerMapper.getBillById(id);
			}
			@Override
			public int modifyBill(Trip_bill bill) {
				// TODO Auto-generated method stub
				return providerMapper.modifyBill(bill);
			}
			@Override
			public int delBill(String id) {
				// TODO Auto-generated method stub
				return providerMapper.delBill(id);
			}
			@Override
			public int providerCount(String proName) {
				return providerMapper.providerCount(proName);
			}
			@Override
			public List<Trip_provider> getProviderList(String proName,int currentPageNo,int pageSize) {
				return providerMapper.getProviderList(proName, (currentPageNo-1)*pageSize, pageSize);
			}	
	
}
